import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const vcot = 'vcode_obtaining_time';
const code = 'vcode';
const vt = 1 * 10 * 1000;
const store = new Vuex.Store({
  state: {
    vCode: '',
  },
  getters: {
    vCode(state) {
      return () => {
        const info = JSON.parse(localStorage.getItem(code));
        console.log(info);
        if (info) {
          const time = localStorage.getItem(vcot);
          if (time) {
            const ftime = Number(vt) + Number(time);
            const ntime = Date.parse(new Date());
            console.log(`${ftime},${ntime}`);
            if (ftime > ntime) {
              console.log('验证码未过期');
              state.vCode = info;
              return state.vCode;
            }
          }
          console.log('验证码已过期');
          localStorage.setItem(code, '');
          state.vCode = '';
          console.log(state.vCode);
        }
        return state.vCode;
      };
    },
  },
  mutations: {
    getCode(state, payload) {
      state.vCode = payload;
      localStorage.setItem(code, JSON.stringify(payload));
      const startTime = Date.parse(new Date());
      localStorage.setItem(vcot, startTime);
    },
  },
  actions: {
    getCode(context, payload) {
      context.commit('getCode', payload);
    },

  },
});
export default store;
